<template>
  <div class="app-container">
    <div class="block">
      <el-button type="success" icon="el-icon-plus" @click.native="add">{{
        $t('button.add')
      }}</el-button>
    </div>

    <el-table :data="data" default-expand-all row-key="id" highlight-current-row border>
      <el-table-column label="名称">
        <template #default="{ row }">
          <el-button type="text" @click="edit(row)">{{ row.name }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="编码" prop="code" />
      <el-table-column label="ID" prop="id" />
      <el-table-column label="是否是菜单" prop="isMenuName" />
      <el-table-column label="URL" prop="url" />
      <el-table-column label="是否启用" prop="statusName" />
      <el-table-column label="顺序" prop="num" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="text" @click="remove(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="formTitle" :visible.sync="formVisible" width="70%">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" minlength="1"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求地址" prop="url">
              <el-input v-model="form.url" minlength="1"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否是菜单">
              <el-radio-group v-model="form.ismenu">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用">
              <el-radio-group v-model="form.status">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单编号" prop="code">
              <el-input v-model="form.code"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="图标">
              <el-input v-model="form.icon"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="num">
              <el-input type="number" v-model="form.num"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="父菜单">
              <el-input
                placeholder="请选择父菜单"
                v-model="form.pname"
                readonly="readonly"
                @click.native="showTree = !showTree"
              >
              </el-input>
              <el-tree
                v-if="showTree"
                empty-text="暂无数据"
                :expand-on-click-node="false"
                :data="data"
                :props="defaultProps"
                @node-click="handleNodeClick"
                class="input-tree"
              >
              </el-tree>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="save">{{ $t('button.submit') }}</el-button>
          <el-button @click.native="formVisible = false">{{ $t('button.cancel') }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getList, save, delMenu } from '@/api/system/menu';

export default {
  name: 'treeTableDemo',
  data() {
    return {
      showTree: false,
      defaultProps: {
        id: 'code',
        label: 'name',
        children: 'children',
      },

      listLoading: true,
      expandAll: false,
      formTitle: '',
      formVisible: false,
      isAdd: false,
      form: {
        id: '',
        pname: '',
        name: '',
        code: '',
        url: '',
        pcode: '',
        ismenu: 1,
        num: 1,
      },
      rules: {
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' },
        ],
        code: [
          { required: true, message: '请输入编码', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' },
        ],
        url: [{ required: true, message: '请输入请求地址', trigger: 'blur' }],
        num: [{ required: true, message: '请输入排序', trigger: 'blur' }],
      },
      data: [],
      selRow: {},
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.fetchData();
    },
    fetchData() {
      this.listLoading = true;
      getList().then((response) => {
        this.data = response.data;
        this.listLoading = false;
      });
    },
    handleNodeClick(data) {
      this.form.pcode = data.code;
      this.form.pname = data.name;
      this.showTree = false;
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true;
      }
      this.$message({
        message: '请选中操作项',
        type: 'warning',
      });
      return false;
    },
    add() {
      this.form = {};
      this.formTitle = '添加菜单';
      this.formVisible = true;
      this.isAdd = true;
    },
    save() {
      var self = this;
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const menuData = self.form;
          menuData.parent = null;
          menuData.children = null;
          save(menuData).then(() => {
            this.$message({
              message: '提交成功',
              type: 'success',
            });
            self.fetchData();
            self.formVisible = false;
          });
        } else {
          return false;
        }
      });
    },
    edit(row) {
      this.form = row;
      if (row.isMenuName === '是') {
        this.form.ismenu = 1;
      } else {
        this.form.ismenu = 0;
      }
      if (row.statusName === '启用') {
        this.form.status = 1;
      } else {
        this.form.status = 0;
      }
      if (row.parent) {
        this.form.pcode = row.parent.code;
        this.form.pname = row.parent.name;
      }
      console.log(this.form.pcode);
      this.formTitle = '编辑菜单';
      this.formVisible = true;
      this.isAdd = false;
    },
    remove(row) {
      console.log(row);
      this.$confirm('确定删除该记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        delMenu(row.id)
          .then(() => {
            this.$message({
              message: '删除成功',
              type: 'success',
            });
            this.fetchData();
          })
          .catch((err) => {
            this.$notify.error({
              title: '错误',
              message: err,
            });
          });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import '~@/styles/common.scss';
</style>
